package com.lesama.buffered;

// 创建对象写入文件，从文件中读取对象

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class BufferedStreamTest2 {
    public static void main(String[] args) throws IOException {
        writeWifeToFile();
        BufferedReader br = new BufferedReader(new FileReader("day14\\wife.txt"));
        ArrayList<Student> students = new ArrayList<>();
        String line;
        while((line = br.readLine()) != null) {
            String[] split = line.split("-");
            students.add(new Student(split[0], Integer.parseInt(split[1])));
        }
        for (Student student : students) {
            System.out.println(student);
        }
    }

    private static void writeWifeToFile() throws IOException {
        Student student1 = new Student("紬", 16);
        Student student2 = new Student("鸥", 16);
        Student student3 = new Student("苍", 16);

        BufferedWriter bw = new BufferedWriter(new FileWriter("day14\\wife.txt"));

        bw.write(student1.getName() + "-" + student1.getAge());
        bw.newLine();
        bw.write(student2.getName() + "-" + student2.getAge());
        bw.newLine();
        bw.write(student3.getName() + "-" + student3.getAge());
        bw.newLine();

        bw.close();
    }
}